<?php

/**
 * This is the model class for table "Ambiente_Uso".
 *
 * The followings are the available columns in table 'Ambiente_Uso':
 * @property integer $ID_AMBIENTE
 * @property integer $ID_USO
 */
class Ambiente_Uso extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Ambiente_Uso the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'Ambiente_Uso';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('ID_AMBIENTE, ID_USO', 'required'),
            array('ID_AMBIENTE, ID_USO', 'numerical', 'integerOnly' => true),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('ID_AMBIENTE, ID_USO', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'iD_USO' => array(self::BELONGS_TO, 'Uso', 'ID_USO'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'ID_AMBIENTE' => 'Ambiente',
            'ID_USO' => 'Uso',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->with = array('iD_USO');

        $criteria->compare('ID_AMBIENTE', $this->ID_AMBIENTE);

        $criteria->compare('ID_USO', $this->ID_USO);

        return new CActiveDataProvider('Ambiente_Uso', array(
            'criteria' => $criteria,
        ));
    }

    public function getUsos() {
        $uso = new Uso();
        $dataprovider = $uso->search();
        $dados = $dataprovider->getData();
        $max = $dataprovider->getItemCount();
        $ret = array();
        for ($i = 0; $i < $max; $i++) {
            $ret += array($dados[$i]['ID_USO'] => $dados[$i]['TX_DESCRICAO']);
        }

        return $ret;
    }

    public function getUsosByAmbiente() {
        $ambiente_Uso = new Ambiente_Uso();
        $ret = array();
        if (isset($_GET['id'])) {
            $ambiente_Uso->ID_AMBIENTE = $_GET['id'];
            $dataprovider = $ambiente_Uso->search();
            $dados = $dataprovider->getData();
            $max = $dataprovider->getItemCount();

            for ($i = 0; $i < $max; $i++) {
                $o = $dados[$i]['iD_USO'];
                $ret += array($dados[$i]['ID_USO'] => $dados[$i]['iD_USO']->TX_DESCRICAO);
            }
        }
        return $ret;
    }

}